<?php

App::uses('PsfmAdmin', 'Lib');
App::uses('FileSystem', 'Base.Lib');
App::uses('ConfigurationKeys', 'ConfigurationKeys.Lib');

class PsfmAdminTest extends CakeTestCase {

    public $fixtures = array(
        'plugin.ConfigurationKeys.SettedConfigurationKey',
        'RawFile',
        'RawFileTag',
        'SourceDocument',
        'SourceDocumentTag',
        'Tag',
    );

    public function setUp() {
        parent::setUp();

        ConfigurationKeys::setKeyValue(
                'raw_files_directory'
                , FileSystem::createTemporaryDirectory()
        );

        ConfigurationKeys::setKeyValue(
                'shared_files_directory'
                , FileSystem::createTemporaryDirectory()
        );

        ConfigurationKeys::setKeyValue(
                'joined_document_regex'
                , '/^([\S]+)/'
        );

        PsfmAdmin::customDataModelsClearCache();
        $this->RawFile = ClassRegistry::init('RawFile');
        $this->RawFileTag = ClassRegistry::init('RawFileTag');
        $this->Tag = ClassRegistry::init('Tag');
        $this->SourceDocument = ClassRegistry::init('SourceDocument');
        $this->SourceDocumentTag = ClassRegistry::init('SourceDocumentTag');
    }

    public function tearDown() {
        FileSystem::recursiveRemoveDirectory(
                ConfigurationKeys::getKeyValue('raw_files_directory')
        );
        FileSystem::recursiveRemoveDirectory(
                ConfigurationKeys::getKeyValue('shared_files_directory')
        );
        parent::tearDown();
    }

    public function testUpdateAllDocumentsTag() {
        $documentCount = 4;
        $fileCountPerDocument = 4;
        for ($doc = 1; $doc <= $documentCount; $doc++) {
            for ($file = 1; $file <= $fileCountPerDocument; $file++) {
                $this->RawFile->create();
                $this->assertNotEqual(
                        $this->RawFile->save(array(
                            $this->RawFile->alias => array(
                                'name' => 'doc' . $doc . ' file' . $file
                            )
                        )), false
                );
            }
        }

        $allDocumentsTag = $this->Tag->findByName(
                ConfigurationKeys::getKeyValue('all_documents_non_tagged_tag_name')
        );
        $this->assertEqual(empty($allDocumentsTag), true);

        PsfmAdmin::updateAllDocumentsTag();

        $allDocumentsTag = $this->Tag->findByName(
                ConfigurationKeys::getKeyValue('all_documents_tag_name')
        );
        $this->assertEqual(empty($allDocumentsTag), false);

        $allDocumentsTagSourceDocumentCount = $this->SourceDocumentTag->find(
                'count', array(
            'conditions' => array(
                $this->SourceDocumentTag->escapeField('tag_id') => $allDocumentsTag['Tag']['id']
            )
                )
        );

        $this->assertEqual($allDocumentsTagSourceDocumentCount, $documentCount);
        $this->assertEqual($allDocumentsTagSourceDocumentCount, $this->SourceDocument->find('count'));
    }

    public function testUpdateAllDocumentsNonTaggedTag() {
        $documentCount = 4;
        $fileCountPerDocument = 4;
        for ($doc = 1; $doc <= $documentCount; $doc++) {
            for ($file = 1; $file <= $fileCountPerDocument; $file++) {
                $this->RawFile->create();
                $this->assertNotEqual(
                        $this->RawFile->save(array(
                            $this->RawFile->alias => array(
                                'name' => 'doc' . $doc . ' file' . $file
                            )
                        )), false
                );
            }
        }

        $allDocumentsNonTaggedTag = $this->Tag->findByName(
                ConfigurationKeys::getKeyValue('all_documents_non_tagged_tag_name')
        );
        $this->assertEqual(empty($allDocumentsNonTaggedTag), true);

        PsfmAdmin::updateAllDocumentsNonTaggedTag();

        $allDocumentsNonTaggedTag = $this->Tag->findByName(
                ConfigurationKeys::getKeyValue('all_documents_non_tagged_tag_name')
        );
        $this->assertEqual(empty($allDocumentsNonTaggedTag), false);

        $allDocumentsTagSourceDocumentCount = $this->SourceDocumentTag->find(
                'count', array(
            'conditions' => array(
                $this->SourceDocumentTag->escapeField('tag_id') => $allDocumentsNonTaggedTag['Tag']['id']
            )
                )
        );

        $this->assertEqual($this->SourceDocument->find('count'), $documentCount);
        $this->assertEqual($allDocumentsTagSourceDocumentCount, $documentCount);

        $otherTag = Basics::saveModelOrThrowException('Tag', array(
                    'Tag' => array(
                        'name' => 'Other tag'
                    )
                ));

        $taggedDocumentCount = intval(ceil($documentCount / 2));
        $sourceDocuments = $this->SourceDocument->find('all', array(
            'limit' => $taggedDocumentCount)
        );

        foreach ($sourceDocuments as $sourceDocument) {
            Basics::saveModelOrThrowException('SourceDocumentTag', array(
                'SourceDocumentTag' => array(
                    'source_document_id' => $sourceDocument['SourceDocument']['id'],
                    'tag_id' => $otherTag['Tag']['id']
                )
            ));
        }

        PsfmAdmin::updateAllDocumentsNonTaggedTag();

        $allDocumentsTagSourceDocumentCount = $this->SourceDocumentTag->find(
                'count', array(
            'conditions' => array(
                $this->SourceDocumentTag->escapeField('tag_id') => $allDocumentsNonTaggedTag['Tag']['id']
            )
                )
        );

        $this->assertEqual($this->SourceDocument->find('count'), $documentCount);
        $this->assertEqual($allDocumentsTagSourceDocumentCount, $documentCount - $taggedDocumentCount);
    }

}